package magicloan.auctionpocket.ui.base;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.adapter.MainFgGvLoanAdapter;
import magicloan.auctionpocket.bean.ProduceBean;

import static magicloan.auctionpocket.R.id.gv_identify_mainfg;

/**
 * Created by hechao on 2018/1/27.
 * <p>
 * GridViewManager  添加HeadView 需要注意的问题  http://blog.csdn.net/qibin0506/article/details/49716795
 */
public class MyReBaseAdapter extends RecyclerView.Adapter {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;

    private ArrayList mArrayList;
    private Context mContext;
    private Activity mActivity;
    private MainFgGvLoanAdapter mGvAdapter ;

    public <T> MyReBaseAdapter(Activity activity, ArrayList<T> reDateList) {
        super();
        mActivity = activity;
        mContext = activity;
        mArrayList = reDateList;
        mGvAdapter = new MainFgGvLoanAdapter(mActivity);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (TYPE_NORMAL == viewType) {
            View nView = View.inflate(mContext, R.layout.fg_first_re_item, null);
            return new MyViewHolder(nView);
        } else if (TYPE_HEADER == viewType) {
            View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.fg_first_re_headv, parent, false);
            return new MyViewHolder_HeadView(layout);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder_HeadView) {
            ArrayList mReDateList = new ArrayList();
            for ( int i = 0 ; i <5 ; i ++ ){
                ProduceBean instance = new ProduceBean();
                mReDateList.add(instance);
            }

            ((MyViewHolder_HeadView) holder).mGridView.setAdapter(mGvAdapter);
            mGvAdapter.setDatas(mReDateList);
            mGvAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
//        if ( mHeadViewRes != -1 )
            return mArrayList == null ? 0 : mArrayList.size() + 1;
//        else
//            return mArrayList == null ? 0 : mArrayList.size();
    }

    @Override
    public int getItemViewType(int position) {
//        if ( mHeadViewRes == -1 )
//            return TYPE_NORMAL;
//        else if ( mHeadViewRes != -1 && position == 0)
//            return TYPE_HEADER;
//        else
//            return TYPE_NORMAL;
        if ( position == 0 )
            return TYPE_HEADER ;
        else
            return TYPE_NORMAL ;
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }

    }

    private class MyViewHolder_HeadView extends RecyclerView.ViewHolder {

        private GridView mGridView;

        public MyViewHolder_HeadView(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(gv_identify_mainfg);
        }

    }

}
